/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.product.service.impl.DaphneProductServiceImpl
   Module Description   :

   Date Created      : 2010/9/17
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.dao.BaseDAO;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.product.entity.DaphneProduct;
import com.yaodian100.ec.product.service.DaphneProductService;

/**
 * @author feeling.wang
 * 
 */
public class DaphneProductServiceImpl extends DomainServiceImpl<DaphneProduct> implements DaphneProductService {
	@Resource
	private BaseManager baseManager;
	@Resource
	private BaseDAO baseDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.product.service.DaphneProductService#importDaphneProduct(java.lang.String, java.lang.String)
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean importDaphneProduct(String importDate, String[] productId) {
		try {
			// 匯入資料前刪除全部的資料
			// Session session = HibernateUtil.getSession();
			// session.beginTransaction();
			//
			// Map<String, Serializable> attrs = new HashMap<String, Serializable>();
			// getDao().executeUpdate("truncate table EC_DAPHNE_PRODUCT", attrs);
			baseDAO.executeSql("truncate table EC_DAPHNE_PRODUCT drop storage", null);
			// 刪除今天之前的資料
			// baseManager.executeHql("delete from DaphneProduct where createDate < sysdate - 1 ", new Object[] {});
			// 刪除importdate的資料
			// baseManager.executeHql("delete from DaphneProduct where  to_char(createDate, 'yyyymmdd')  = ? ",
			// new Object[] { importDate });
			List<DaphneProduct> daphneProducts = new ArrayList<DaphneProduct>();
			DaphneProduct daphne = null;
			for (int i = 0; i < productId.length; i++) {
				daphne = new DaphneProduct();
				daphne.setProductId(productId[i]);
				daphneProducts.add(daphne);
			}
			if (!daphneProducts.isEmpty()) {
				getDao().saveOrUpdateBatch(daphneProducts);
			}
		} catch (Exception e) {
			logger.info("exception:{}", e.fillInStackTrace());
			return false;
		}
		return true;
	}

	public BaseManager getBaseManager() {
		return baseManager;
	}

	public void setBaseManager(BaseManager baseManager) {
		this.baseManager = baseManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.product.service.DaphneProductService#getAvailableDaphneProductMap()
	 */
	@Override
	public DaphneProduct getAvailableDaphneProductById(String productId) {
		CommonCriteria crit = new CommonCriteria();
		crit.addEq("productId", productId);
		List<DaphneProduct> dps = getDao().getList(crit, new String[] { "createDate desc" });
		if (null != dps && !dps.isEmpty() && dps.size() > 0) {
			return dps.get(0);
		} else {
			return null;
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<String> getAvailableDaphneCategory() {
		List<String> categoryList = new ArrayList<String>();
		StringBuffer sb = new StringBuffer();
		sb.append("Select Distinct Cm.Category_Path From ");
		sb.append("Ec_Daphne_Product Dp, Ec_Category_Main Cm, Ec_Product_Main Pm, EC_VIEW_SHELF Vs ");
		sb.append("Where Pm.Main_Category_Id = Cm.Category_Id ");
		sb.append("and Dp.Product_Id = Pm.Product_Id ");
		sb.append("and Pm.PRODUCT_ID = Vs.PRODUCT_ID ");
		sb.append("and to_char(dp.create_Date, 'yyyymmdd') = to_char(sysdate, 'yyyymmdd') ");
		sb.append("and Cm.IS_DELETE != 'Y' ");
		List<Object> list = getDao().getSQLQueryByList(sb.toString(), null, 0, -1);
		for (Object data : list) {
			String[] tmp = data.toString().split("/");
			if (tmp.length >= 2) {
				if (!categoryList.contains(tmp[0])) {
					categoryList.add(tmp[0]);
				}
				if (!categoryList.contains(tmp[1])) {
					categoryList.add(tmp[1]);
				}
			}
		}
		return categoryList;
	}
}
